package com.cyam.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.cyam.domain.Article;
import com.cyam.mapper.ArticleMapper;
import com.cyam.service.ArticleService;
import com.cyam.util.Pager;
/**
 * 文章服务层实现
 * @author CourageAQ
 *
 */
@Controller
@Transactional
public class ArticleServiceImpl implements ArticleService {
	
	@Resource
	private ArticleMapper articleMapper;

	@Override
	public void save(Article t) throws Exception {
		articleMapper.insertObject(t);
	}

	@Override
	public void update(Article t) throws Exception {
		articleMapper.modifyObject(t);
	}

	@Override
	public void remove(int id) throws Exception {
		articleMapper.deleteObject(id);
	}

	@Override
	public List<Article> findAll(Map<String, Object> map) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Article findOne(Map<String, Object> map) throws Exception {
		return articleMapper.selectOne(map);
	}

	@Override
	public Pager findPager(Map<String, Object> map, int page, int size) throws Exception {
		map.put("beginPageCount", size * (page-1));
		map.put("pageSize", size);
		List<Article> list = articleMapper.selectPage(map);
		int count = articleMapper.countObject(map);
		Pager pager = new Pager(size, page, count, list);
		return pager;
	}

	@Override
	public Pager countPager(Map<String, Object> map,int page, int size) throws Exception {
		int count = articleMapper.countObject(map);
		Pager pager = new Pager(size, page, count);
		return pager;
	}

	/**网站主页，根据点击量，显示最高的八条数据*/
	@Override
	public List<Article> listArticleByClicknum() throws Exception {
		List<Article> list = articleMapper.selectPage2(null);
		return list;
	}
	/**后台查看列表*/
	@Override
	public Pager selectPage3(Map<String, Object> map, int page, int size) throws Exception {
		map.put("beginPageCount", size * (page-1));
		map.put("pageSize", size);
		List<Article> list = articleMapper.selectPage3(map);
		int count = articleMapper.countObject3(map);
		Pager pager = new Pager(size, page, count, list);
		return pager;
	}


	@Override
	public Pager countPager3(Map<String, Object> map, int page, int size) throws Exception {
		int count = articleMapper.countObject3(map);
		Pager pager = new Pager(size, page, count);
		return pager;
	}

	@Override
	public List<Article> selectByTime(Map<String, Object> map) throws Exception {
		return articleMapper.selectByTime(map);
	}

	@Override
	public Pager countByTime2(Map<String, Object> map, int page, int size) throws Exception {
		int count = articleMapper.countByTime2(map);
		Pager pager = new Pager(size, page, count);
		return pager;
	}
	@Override
	public Pager selectByTime2(Map<String, Object> map,int page, int size) throws Exception {
		map.put("beginPageCount", size * (page-1));
		map.put("pageSize", size);
		List<Article> list = articleMapper.selectByTime2(map);
		int count = articleMapper.countByTime2(map);
		Pager pager = new Pager(size, page, count, list);
		return pager;
	}

	@Override
	public List<Article> listOfTimeTop5() throws Exception {
		return articleMapper.listOfTimeTop5();
	}

	@Override
	public List<Article> listRandTop5() throws Exception {
		return articleMapper.listRandTop5();
	}

	@Override
	public Pager searchCountArticle(Map<String, Object> map, int page, int size) throws Exception {
		int count = articleMapper.searchCountArticle(map);
		Pager pager = new Pager(size, page, count);
		return pager;
	}
	@Override
	public Pager searchListArticle(Map<String, Object> map,int page, int size) throws Exception {
		map.put("beginPageCount", size * (page-1));
		map.put("pageSize", size);
		List<Article> list = articleMapper.searchListArticle(map);
		int count = articleMapper.searchCountArticle(map);
		Pager pager = new Pager(size, page, count, list);
		return pager;
	}
}
